package com.shunwang.tools.httpclientsupport.support;

import java.io.IOException;

import org.apache.commons.httpclient.HttpException;

import com.shunwang.tools.httpclientsupport.exception.HttpClientException;
import com.shunwang.tools.httpclientsupport.pool.Pool;

public abstract class AbstractClientExector implements ClientExecutor {
	
	private final Pool pool;
	
	public AbstractClientExector(Pool pool){
		this.pool = pool;
	}
	
	public Object execute(ClientCallBack method){
		Object client = null;
		try{
			client = pool.getResource();
			return doExecute(method,client);
		}catch(Exception e){
			throw new HttpClientException(e);
		}finally{
			if(client != null){
				pool.returnResource(client);
			}
		}
	}
	
	protected Object doExecute(ClientCallBack method,Object client)throws HttpException, IOException,Exception{
		return method.doClient(client);
	}

}
